package com.chen.Dome8Test;

import java.util.List;

//线程类
public class PeopleGetRedMoney extends Thread {
    private List<Integer> list;

    public PeopleGetRedMoney(List<Integer> list, String s) {
        super(s);
        this.list = list;
    }

    @Override
    public void run() {
        //100个人来抢红包
        String name = Thread.currentThread().getName();
        while (true) {

            synchronized (list) {
                if (list.size() == 0) {
                    break;
                }
                //模拟抢红包，随机一个索引
                int index = (int) (Math.random() * list.size());
                Integer money = list.remove(index);
                System.out.println(name + "抢到了" + money + "元");
                if (list.size() == 0) {
                    System.out.println("红包被抢完了");
                    break;
                }
            }
        }
    }
}
